﻿using System.IO;
using System.Reflection;
using System.Web;

namespace SuperCaptcha.Mvc.Infrastructure
{
    internal sealed class Captcha : IHtmlString
    {
        private IHtmlString _injectionString = null;

        internal Captcha(IHtmlString injection)
        {
            _injectionString = injection;
        }

        public string ToHtmlString()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string fileName = string.Format("{0}{1}", assembly.ManifestModule.Name.Replace("dll", string.Empty), "Resources.Captcha.cshtml");
            return CreateHtml(assembly, fileName);
        }

        private string CreateHtml(Assembly assembly, string fileName)
        {
            StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(fileName));
            var baseValue = reader.ReadToEnd();
            return baseValue.Replace("[TextControl]", _injectionString.ToHtmlString());
        }
    }
}
